package com.alogic.ac.loader.acm.xscript;

import com.alogic.ac.AccessControlModel;
import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * 增加ACL
 *
 * @since 1.6.12.54 [20191219]
 */
public class ACMAddAci extends AbstractLogiclet {
    protected String pid = "$acm-object";
    protected String $ip = "*";
    protected String $service = "*";
    protected String $maxThread = "10";
    protected String $timesPerMin = "100000";
    protected String $priority = "0";

    public ACMAddAci(String tag, Logiclet p) {
        super(tag, p);
    }

    public void configure(Properties p){
        super.configure(p);
        pid = PropertiesConstants.getString(p,"pid", pid,true);

        $maxThread = PropertiesConstants.getRaw(p, "maxThread", $maxThread);
        $timesPerMin = PropertiesConstants.getRaw(p, "timesPerMin", $timesPerMin);
        $priority = PropertiesConstants.getRaw(p, "priority", $priority);
        $ip = PropertiesConstants.getRaw(p, "ip", $ip);
        $service = PropertiesConstants.getRaw(p, "service", $service);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx,
                             ExecuteWatcher watcher) {
        AccessControlModel acm = ctx.getObject(pid);
        if (acm == null){
            log("the acm object is null:" + pid,LOG_WARNING,ctx);
            return ;
        }

        acm.addACI(
                PropertiesConstants.transform(ctx,$ip,"*"),
                PropertiesConstants.transform(ctx,$service,"*"),
                PropertiesConstants.transform(ctx,$maxThread,10),
                PropertiesConstants.transform(ctx,$timesPerMin,100000),
                PropertiesConstants.transform(ctx,$priority,0)
        );
    }
}
